package com.bdqn.wf4.cinconsumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.bdqn.wf4.cinbase.pojo.Film;
import com.bdqn.wf4.cinbase.service.FilmService;
import com.bdqn.wf4.cinbase.util.PageData;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class FilmController {
    @Reference
    FilmService filmService;
    @RequestMapping("show")
    public String Index(
            @RequestParam(value = "uname", required = false) String uname,
            @RequestParam(value = "actstar", required = false) String actstar,
            @RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex,
            Model model) {
        int pageSize = 3;
        PageData<Film> pageData = filmService.getPageData(uname, actstar,
                pageIndex, pageSize);
        model.addAttribute("pageData", pageData);
        model.addAttribute("uname", uname);
        model.addAttribute("actstar", actstar);
        return "index";
    }

    @RequestMapping(value = "checkadd", method = RequestMethod.GET, produces = { "application/json;charset=utf-8" })
    @ResponseBody
    public String Check(Film film) {
        int update = filmService.update(film);
        String str = JSON.toJSONString(update);
        return str;
    }

    @RequestMapping("toadd")
    public String toPing(int id, Model model) {
        Film byId = filmService.getById(id);
        model.addAttribute("byId", byId);
        return "add";
    }

    @RequestMapping("add")
    public String Ping(Film film) {
        int con = filmService.uadd(film);
        if (con == 1) {
            return "redirect:show";
        }
        return "redirect:toadd";
    }
}
